package com.ohuang.test_compose.page

import android.annotation.SuppressLint
import android.util.Log
import android.widget.Toast
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.ExperimentalUnitApi
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.ohuang.test_compose.MainViewModel
import kotlinx.coroutines.delay

/**
 *
 *
 */
@ExperimentalPermissionsApi
@InternalComposeApi
@ExperimentalUnitApi
@ExperimentalMaterialApi
@ExperimentalAnimationApi
@Composable
fun EffectPage(viewModel: MainViewModel = androidx.lifecycle.viewmodel.compose.viewModel()) {
    var current = LocalContext.current
    var string by remember {
        mutableStateOf("123")
    }
    Column() {
        Text(text = string)
        LandingScreen(string) {
            string = "${System.currentTimeMillis()}"
        }
        val rememberAnalytics = rememberAnalytics(string)
        LaunchedEffect(Unit) {
            viewModel.map = rememberAnalytics
        }
        Text(text = "点击查看map", Modifier.clickable {
            Toast.makeText(current, "${viewModel.map}", Toast.LENGTH_SHORT).show()
        })

        val loadUrl = loadUrl("http://www.baidu.com")
        Text(text = loadUrl.value)
        LaunchedEffectTest()
    }


}

@Composable
fun LaunchedEffectTest(){
    var current = LocalContext.current
    var isLaunched by remember {
        mutableStateOf(true)
    }

    LaunchedEffect(key1 = Unit, block = {
        Log.d("EffectPage", "LaunchedEffectTest: key=Unit")
    })//只会执行一次

    if (isLaunched) {//退出重组后 可重新执行
        LaunchedEffect(key1 = Unit, block = {
            Toast.makeText(current, "LaunchedEffect", Toast.LENGTH_SHORT).show()

        })
    }
    LaunchedEffect(key1 = isLaunched, block = {  //和上次的值不同就会重组
        Log.d("EffectPage", "LaunchedEffectTest: key=$isLaunched")
    })

    LaunchedEffect(key1 = if (isLaunched) "1" else "2", block = { //和上次的值不同就会重组
        Log.d("EffectPage", "LaunchedEffectTest: key=${if (isLaunched) "1" else "2"}")
    })


    Button(onClick = { isLaunched=!isLaunched }) {
        Text(text = "当前状态:" +isLaunched )
    }
}


@Composable
fun LandingScreen(name: String, onTimeout: () -> Unit) {
    var string by remember {
        mutableStateOf("点击")
    }
    // This will always refer to the latest onTimeout function that
    // LandingScreen was recomposed with
    val currentOnTimeout by rememberUpdatedState(onTimeout)
    val nameCurrent by rememberUpdatedState(name)

    // Create an effect that matches the lifecycle of LandingScreen.
    // If LandingScreen recomposes, the delay shouldn't start again.
    LaunchedEffect(Unit) {
        delay(3000)
        currentOnTimeout() //通过rememberUpdatedState获取最新的重组数据  调用onTimeout只会调用第一次的数据
        delay(3000)
        Log.d("EffectPage", "LandingScreen: name=$name")
        Log.d("EffectPage", "LandingScreen: nameCurrent=$nameCurrent")
    }
    LaunchedEffect(key1 = name){
        Log.d("EffectPage", "LandingScreen:LaunchedEffect $name")
        delay(5000)
        Log.d("EffectPage", "LandingScreen:LaunchedEffect $name 完成")
    }
    LaunchedEffect(key1 = Unit){
        Log.d("EffectPage", "LandingScreen:LaunchedEffect Unit $name")
        delay(5000)
        Log.d("EffectPage", "LandingScreen:LaunchedEffect Unit $name 完成")
    }

    Button(onClick = { string = "已点击" }) {
        Text(text = "${name}:" + string)
    }


}

@Composable
fun rememberAnalytics(user: String): Map<String, String> {
    val analytics: HashMap<String, String> = remember {
        HashMap()
    }

    SideEffect {  //每次重组完成后都会调用
        analytics["user"] = user
    }
    analytics["user"] = "sb"
    return analytics
}


@SuppressLint("ProduceStateDoesNotAssignValue")
@Composable
fun loadUrl(url: String): State<String> {
    //将非 Compose 状态转换为 Compose 状态
    return produceState<String>(initialValue = "正在请求:$url", key1 = url) {
        delay(3000)
        value= "请求成功:$url"
    }
}

